package com.logos.musicframe.chatmemory;

import com.logos.musicframe.constant.RedisConstant;
import com.logos.musicframe.utils.RedisUtil;
import com.logos.musicframe.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author logos
 * date 2025/6/17 15:09
 * @version v1.0
 * @Package com.logos.musicframe.chatmemory
 */
@Component
public class RedisChatHistory implements ChatHistoryRepository{

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserInfoUtil userInfoUtil;

    @Override
    public void save(String chatId, String userId) {
            redisUtil.addToSet(RedisConstant.CHAT_HISTORY_USER + userId, chatId);
    }

    @Override
    public List<String> getChatIdList(String userId) {
        Set<String> chatIds = redisUtil.getAllFromSet(RedisConstant.CHAT_HISTORY_USER + userId);
        if(chatIds == null || chatIds.isEmpty()) {
            return Collections.emptyList();
        }
        return chatIds.stream().sorted(String::compareTo).toList();
    }

}
